﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Route_Country_Entity: EntityBase<ROUTE_COUNTRY>
    {
        public override RESP_Info Add(ROUTE_COUNTRY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into ROUTE_COUNTRY(ROUTE_ID,ROUTE_CONTINENT,ROUTE_COUNTRY_NAME,ROUTE_CITY) values (@ROUTE_ID,@ROUTE_CONTINENT,@ROUTE_COUNTRY_NAME,@ROUTE_CITY)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_CONTINENT", model.ROUTE_CONTINENT));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_COUNTRY_NAME", model.ROUTE_COUNTRY_NAME));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_CITY", model.ROUTE_CITY));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加行程站点成功</p></h5>";
                    info.Url = "CityList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加行程站点失败</p></h5>";
                    info.Url = "CityAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加行程站点失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(ROUTE_COUNTRY model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update ROUTE_COUNTRY set ROUTE_CONTINENT=@ROUTE_CONTINENT,ROUTE_COUNTRY_NAME=@ROUTE_COUNTRY_NAME,ROUTE_CITY=@ROUTE_CITY where ROUTE_ID=@ROUTE_ID and ROUTE_COUNTRY=@ROUTE_COUNTRY";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_CONTINENT", model.ROUTE_CONTINENT));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_COUNTRY_NAME", model.ROUTE_COUNTRY_NAME));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_CITY", model.ROUTE_CITY));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑行程站点成功</p></h5>";
                    info.Url = "CityList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑行程站点失败</p></h5>";
                    info.Url = "CityAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑行程站点失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(ROUTE_COUNTRY routeCountry)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from ROUTE_COUNTRY where ROUTE_ID=@ROUTE_ID and ROUTE_CITY=@ROUTE_CITY";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", routeCountry.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_CITY", routeCountry.ROUTE_CITY));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除行程站点成功</p></h5>";
                    info.Url = "CityList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除行程站点失败</p></h5>";
                    info.Url = "CityList.aspx";

                    Tom.Net.Log.LogManager.Error("删除行程站点失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override ROUTE_COUNTRY GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select ROUTE_ID,ROUTE_CONTINENT,ROUTE_COUNTRY_NAME,ROUTE_CITY from ROUTE_COUNTRY where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTE_COUNTRY routeCountry = new ROUTE_COUNTRY();
                        if (!reader.IsDBNull(0)) routeCountry.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeCountry.ROUTE_CONTINENT = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routeCountry.ROUTE_COUNTRY_NAME = reader.GetString(2);
                        if (!reader.IsDBNull(3)) routeCountry.ROUTE_CITY = reader.GetString(3);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeCountry;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程站点数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override ROUTE_COUNTRY GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTE_COUNTRY routeCountry = new ROUTE_COUNTRY();
                        if (!reader.IsDBNull(0)) routeCountry.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeCountry.ROUTE_CONTINENT = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routeCountry.ROUTE_COUNTRY_NAME = reader.GetString(2);
                        if (!reader.IsDBNull(3)) routeCountry.ROUTE_CITY = reader.GetString(3);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeCountry;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程站点数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<ROUTE_COUNTRY> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<ROUTE_COUNTRY> rcs = new List<ROUTE_COUNTRY>();
                    while (reader.Read())
                    {
                        ROUTE_COUNTRY routeCountry = new ROUTE_COUNTRY();
                        if (!reader.IsDBNull(0)) routeCountry.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeCountry.ROUTE_CONTINENT = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routeCountry.ROUTE_COUNTRY_NAME = reader.GetString(2);
                        if (!reader.IsDBNull(3)) routeCountry.ROUTE_CITY = reader.GetString(3);

                        rcs.Add(routeCountry);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return rcs;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取行程站点数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}

